package com.example.watertest;
import java.io.IOException;
import java.util.Calendar;
 
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.particle.BatchedSpriteParticleSystem;
import org.andengine.entity.particle.ParticleSystem;
import org.andengine.entity.particle.emitter.CircleOutlineParticleEmitter;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.emitter.RectangleOutlineParticleEmitter;
import org.andengine.entity.particle.initializer.AccelerationParticleInitializer;
import org.andengine.entity.particle.initializer.AlphaParticleInitializer;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ExpireParticleInitializer;
import org.andengine.entity.particle.initializer.GravityParticleInitializer;
import org.andengine.entity.particle.initializer.RotationParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.ParallaxBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.UncoloredSprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.CardinalSplineMoveModifier;
import org.andengine.entity.modifier.CardinalSplineMoveModifier.CardinalSplineMoveModifierConfig;
import org.andengine.entity.modifier.ColorModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.entity.modifier.JumpModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveByModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.input.touch.TouchEvent;

import android.graphics.Color;
import android.opengl.GLES20;
import android.util.Log;
 
 
 
public class MainActivityParticulesSystems extends SimpleBaseGameActivity implements IOnSceneTouchListener {
 
protected static final IAnimationListener AnimatedSprite = null;
private Camera mCamera;
private static int WIDTH = 800;
private static int HEIGHT= 480;
private Font miFuente;
private BitmapTextureAtlas miAtlas;
private ITextureRegion texturaChar, texturaBoton;
private Sprite charSprite, Boton;
private ITiledTextureRegion texturaAnimada;
private AnimatedSprite spriteAnimado;
private Text miTexto;
private int cuenta = 0;

@Override
public EngineOptions onCreateEngineOptions() {
 
 
// Definimos nuestra camara
mCamera = new Camera(0, 0, WIDTH, HEIGHT);
// Ahora declaramos las opciones del motor
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
//EngineOptions(es full screen?, Cual es la orientacion de la pantalla?, Como actuaremos ante distintas resoluciones?, y la camara)
// impedimos que la pantalla se apague por inactividad
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
// Return the engineOptions object, passing it to the engine
return engineOptions;
}
 
@Override
protected void onCreateResources() throws IOException {
 
//primero debemos indicar donde estan las imagenes
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
//luego crear el atlas, darle medidas y un tipo de textura.
miAtlas = new BitmapTextureAtlas(getTextureManager(), 800, 800, TextureOptions.BILINEAR);
//ubicamos nuestra imagen en el atlas
//texturaChar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "waterman.png", 0, 0,13,3);
//texturaChar= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "char.png",39, 56, 4, 10);
//Aqui ubicamos el sprite para que no se ubique sobre el anterior
//Osea teniendo en cuenta que el anterior estaba en 0,0 y ocupa hasta 75,95
//Lo ubicaremos despues 76 en x, 0 en y, e indicamos que tiene 10 columnas y 10 filas.
texturaAnimada = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "watermanOK.png", 70, 60, 12, 3);
texturaBoton = BitmapTextureAtlasTextureRegionFactory.createFromAsset(miAtlas, this, "boton.png", 0, 200);

//y la cargamos
miAtlas.load();
//Aqui hemos creado un atlas de 256 x 256 para cargar una imagen de 108 x 253
//en el punto 0,0 de miAtlas, osea nos sobra espacio


//Indicamos donde se ubican las fuentes
FontFactory.setAssetBasePath("fuentes/");
//aqui creamos la textura donde cargaremos la fuente
final ITexture fontTexture = new BitmapTextureAtlas(getTextureManager(), 256, 256, TextureOptions.BILINEAR);
//Aqui definimos cual es la fuente, tamaño, si usara antialias y su color
miFuente = FontFactory.createFromAsset(getFontManager(), fontTexture, getAssets(), "fuente.ttf", 40, true, Color.BLUE);
//La cargamos
miFuente.load();



}


 
@Override
protected Scene onCreateScene() {
 
Scene sceneEjemplo = new Scene();


/* Define the center point of the particle system spawn location */
final int particleSpawnCenterX = (int) (WIDTH * 0.5f);
final int particleSpawnCenterY = (int) (HEIGHT * 0.5f);

/* Create the particle emitter */
PointParticleEmitter particleEmitter = new PointParticleEmitter(particleSpawnCenterX, particleSpawnCenterY);
/* Define the radius of the circle for the particle emitter */
 float particleEmitterRadius = 50;

/* Create the particle emitter */
CircleOutlineParticleEmitter particleEmitter2 = new CircleOutlineParticleEmitter(particleSpawnCenterX, particleSpawnCenterY, particleEmitterRadius);


/* Define the width and height of the rectangle particle emitter */
final float particleEmitterWidth = 50;
final float particleEmitterHeight = 100;

/* Create the particle emitter */
RectangleOutlineParticleEmitter particleEmitter3 = new RectangleOutlineParticleEmitter(particleSpawnCenterX, particleSpawnCenterY, particleEmitterWidth, particleEmitterHeight);

/* Define the particle system properties */
 float minSpawnRate = 25;
 float maxSpawnRate = 50;
 int maxParticleCount = 150;

/* Create the particle system */
BatchedSpriteParticleSystem particleSystem = new BatchedSpriteParticleSystem(
    particleEmitter2, minSpawnRate, maxSpawnRate, maxParticleCount,
    texturaBoton,
    getVertexBufferObjectManager());


/* Add an acceleration initializer to the particle system */
particleSystem.addParticleInitializer(new AccelerationParticleInitializer<UncoloredSprite>(25f, -25f, 50f, 100f));

/* Add an expire initializer to the particle system */
particleSystem.addParticleInitializer(new ExpireParticleInitializer<UncoloredSprite>(4));

/* Add a particle modifier to the particle system */
particleSystem.addParticleModifier(new ScaleParticleModifier<UncoloredSprite>(0f, 3f, 0.2f, 1f));

particleSystem.addParticleInitializer(new AlphaParticleInitializer<UncoloredSprite>(0.5f, 1));
particleSystem.addParticleInitializer(new RotationParticleInitializer<UncoloredSprite>(0, 359));

/* Attach the particle system to the Scene */
sceneEjemplo.attachChild(particleSystem);



//(posicion x, posicion y, textura, elemento de andengine ignorenlo)
//charSprite = new Sprite(100, 200, texturaChar, getVertexBufferObjectManager());
spriteAnimado = new AnimatedSprite(100, 150,texturaAnimada, getVertexBufferObjectManager());
Boton = new Sprite(100, 100, texturaBoton, getVertexBufferObjectManager()){
	
	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {
		//(posicion x, posicion y, textura, elemento de andengine ignorenlo) - igual que arriba
		
			//Crearemos nuestro modifier, en este caso un JumpModifier (hay otros, movetoxmodifier, alphamodifier,etc etc etc)
			//JumpModofier(tiempo del salto, desdeX, hastaX, desdeY, hastaY, altura del Salto);	
		//spriteAnimado.registerEntityModifier(new MoveXModifier(1,120, 200));	
		//marcianoAnimado = new AnimatedSprite(39, 56,texturaChar, getVertexBufferObjectManager());
		//para animar el sprite debemos otorgarle tiempos en milisegundos a cada frame
		long[] duracionFrame = { 200, 200, 200, 200, 200, 200, 200};
		int[] Frames = { 6, 0, 1, 2, 3, 4, 5 };
		//long[] duracionFrames = { 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500};
		//( tiempos x frame, del frame 1 al 6, siempre repitiendose)
		//spriteAnimado.animate(duracionFrame, 6, 9, false);
		spriteAnimado.animate(duracionFrame, Frames , false, new IAnimationListener(){
			
			 @Override
		      public void onAnimationStarted(AnimatedSprite pAnimatedSprite,
		          int pInitialLoopCount) {
		        /* Fired when the animation first begins to run*/
	
				 
		      }

			@Override
			public void onAnimationFrameChanged(
					org.andengine.entity.sprite.AnimatedSprite pAnimatedSprite,
					int pOldFrameIndex, int pNewFrameIndex) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationLoopFinished(
					org.andengine.entity.sprite.AnimatedSprite pAnimatedSprite,
					int pRemainingLoopCount, int pInitialLoopCount) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationFinished(
					org.andengine.entity.sprite.AnimatedSprite pAnimatedSprite) {
				// TODO Auto-generated method stub
				
			}
			
		});
		//charSprite.detachSelf();
			
		
		
		return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
	}
	
};
//nunca se olviden de esto
sceneEjemplo.registerTouchArea(Boton);
sceneEjemplo.attachChild(Boton);

spriteAnimado.registerEntityModifier(new ScaleModifier(0.5f, 1, 2));


//marcianoAnimado.animate(duracionFrames, 1, 11, true);
//y lo añadimos a la escena
sceneEjemplo.attachChild(spriteAnimado);

//sceneEjemplo.attachChild(marcianoAnimado);

 
//sceneEjemplo.attachChild(charSprite);
return sceneEjemplo;


}

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
 
  
  return false;
}
 
}